/**
 * Environment and WeChat Work Detection Utility
 * 环境和企业微信检测工具
 * 
 * This utility provides functions to detect the current environment
 * and check if the application is running in WeChat Work.
 * In production environment, the application will only be accessible
 * within WeChat Work browser.
 * 
 * 此工具提供检测当前环境和企业微信的功能。
 * 在生产环境下，应用只能在企业微信浏览器中访问。
 */

/**
 * Check if the current environment is production
 * 检查当前是否为生产环境
 * @returns {boolean} True if in production environment
 */
export const isProduction = () => {
  return import.meta.env.PROD || process.env.NODE_ENV === 'production'
}

/**
 * Check if the current environment is development
 * 检查当前是否为开发环境
 * @returns {boolean} True if in development environment
 */
export const isDevelopment = () => {
  return import.meta.env.DEV || process.env.NODE_ENV === 'development'
}

/**
 * Check if running in WeChat Work environment
 * 检查是否在企业微信环境中运行
 * @returns {boolean} True if in WeChat Work
 */
export const isWeChatWork = () => {
  const ua = navigator.userAgent.toLowerCase()
  return ua.includes('wxwork') || ua.includes('micromessenger')
}

/**
 * Check if running in regular WeChat (not WeChat Work)
 * 检查是否在普通微信环境中运行（非企业微信）
 * @returns {boolean} True if in regular WeChat
 */
export const isWeChat = () => {
  const ua = navigator.userAgent.toLowerCase()
  return ua.includes('micromessenger') && !ua.includes('wxwork')
}

/**
 * Check if the application should enforce WeChat Work access
 * 检查应用是否应该强制企业微信访问
 * @returns {boolean} True if should enforce WeChat Work access
 */
export const shouldEnforceWeChatWork = () => {
  return isProduction()
}

/**
 * Check if the current access is allowed based on environment rules
 * 根据环境规则检查当前访问是否被允许
 * @returns {boolean} True if access is allowed
 */
export const isAccessAllowed = () => {
  // In development environment, allow all access
  // 开发环境下允许所有访问
  if (isDevelopment()) {
    return true
  }
  
  // In production environment, only allow WeChat Work access
  // 生产环境下只允许企业微信访问
  if (isProduction()) {
    return isWeChatWork()
  }
  
  // Default to allow access
  // 默认允许访问
  return true
}

/**
 * Get environment information for debugging
 * 获取环境信息用于调试
 * @returns {object} Environment information
 */
export const getEnvironmentInfo = () => {
  return {
    isProduction: isProduction(),
    isDevelopment: isDevelopment(),
    isWeChatWork: isWeChatWork(),
    isWeChat: isWeChat(),
    shouldEnforceWeChatWork: shouldEnforceWeChatWork(),
    isAccessAllowed: isAccessAllowed(),
    userAgent: navigator.userAgent,
    nodeEnv: process.env.NODE_ENV,
    viteMode: import.meta.env.MODE
  }
}

/**
 * Show access denied message
 * 显示访问被拒绝的消息
 */
export const showAccessDeniedMessage = () => {
  const message = `
    Access Denied / 访问被拒绝   
  `
  // Create a styled error page
  // 创建一个样式化的错误页面
  document.body.innerHTML = `
    <div style="
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      min-height: 100vh;
      padding: 2rem;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
      color: white;
      text-align: center;
    ">
      <div style="
        background: rgba(255, 255, 255, 0.1);
        backdrop-filter: blur(10px);
        border-radius: 16px;
        padding: 3rem;
        max-width: 500px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
      ">
        <div style="font-size: 4rem; margin-bottom: 1rem;">🚫</div>
        <h1 style="margin: 0 0 1rem 0; font-size: 1.5rem; font-weight: 600;">Access Denied</h1>
        <h2 style="margin: 0 0 2rem 0; font-size: 1.2rem; font-weight: 400;">访问被拒绝</h2>
      </div>
    </div>
  `
}

export default {
  isProduction,
  isDevelopment,
  isWeChatWork,
  isWeChat,
  shouldEnforceWeChatWork,
  isAccessAllowed,
  getEnvironmentInfo,
  showAccessDeniedMessage
}